<template>
  <div class="contributor-list">
    <h3 class="list-title">{{ title }}</h3>
    <div class="contributor-grid">
      <div 
        v-for="contributor in contributors" 
        :key="contributor.id || contributor.name" 
        class="contributor-card"
      >
        <!-- 使用一个 v-if/v-else 确保头像逻辑正确 -->
        <template v-if="contributor.avatarUrl && contributor.showImage !== false">
          <img 
            :src="contributor.avatarUrl" 
            class="avatar-img" 
            :alt="contributor.name" 
            @error="onImageError(contributor)"
          />
        </template>
        <template v-else>
          <div 
            class="avatar-fallback" 
            :style="{ backgroundColor: getBgColor(contributor.name) }"
          >
            {{ getFirstChar(contributor.name) }}
          </div>
        </template>

        <div class="contributor-info">
          <a 
            :href="contributor.profileUrl" 
            target="_blank" 
            rel="noopener noreferrer" 
            class="contributor-name"
          >
            {{ contributor.name }}
          </a>
          <p class="contributor-summary">{{ contributor.summary }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps } from 'vue'

defineProps({
  title: String,
  contributors: {
    type: Array,
    default: () => []
  }
})

/**
 * @description: 工具函数：获取名字的第一个大写字符（支持中英文）
 * @param {string} name 贡献者姓名
 * @return {string} 姓名的首字母大写或 '?'
 */
const getFirstChar = (name) => {
  if (!name || typeof name !== 'string') {
    return '?'
  }
  return name.trim().charAt(0).toUpperCase()
}

/**
 * @description: 工具函数：根据字符串生成一致的哈希颜色
 * @param {string} str 用于生成颜色的字符串，通常是贡献者姓名
 * @return {string} 颜色值 (HSL 格式)
 */
const getBgColor = (str) => {
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    hash = str.charCodeAt(i) + ((hash << 5) - hash);
  }
  const h = hash % 360;
  return `hsl(${h}, 70%, 50%)`;
}

/**
 * @description: 处理图片加载失败事件，将贡献者的 showImage 属性设置为 false
 * @param {object} contributor 贡献者对象
 */
const onImageError = (contributor) => {
  // 使用 Vue 的响应式更新，通过给对象添加新属性来触发重新渲染
  contributor.showImage = false;
}
</script>

<style scoped>
/* 核心列表和卡片样式 */
.contributor-list {
  margin-bottom: 40px;
}

.list-title {
  font-size: 1.5rem;
  font-weight: 500;
  color: #fff;
  margin-bottom: 20px;
  border-bottom: 1px solid #555;
  padding-bottom: 5px;
}

.contributor-grid {
  display: grid;
  gap: 20px;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}

.contributor-card {
  background-color: rgba(255, 255, 255, 0.05);
  border-radius: 12px;
  padding: 15px;
  display: flex;
  align-items: center;
  gap: 15px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.contributor-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 15px rgba(0,0,0,0.3);
}

/* 图片头像样式 */
.avatar-img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  flex-shrink: 0;
  object-fit: cover;
  background-color: #444; /* Fallback for broken images */
}

/* 文字头像样式 */
.avatar-fallback {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  /* 确保文字居中 */
  align-items: center;
  justify-content: center;
  font-weight: bold;
  color: #fff;
  font-size: 1.2rem;
  text-transform: uppercase; /* 确保文字大写 */
}

/* 贡献者信息区 */
.contributor-info {
  display: flex;
  flex-direction: column;
}

.contributor-name {
  color: #fff;
  text-decoration: none;
  font-weight: 500;
  font-size: 1rem;
}

.contributor-name:hover {
  text-decoration: underline;
}

.contributor-summary {
  color: #aaa;
  font-size: 0.9rem;
  margin: 4px 0 0 0;
}
</style>
